﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dm.Model.V20151123;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Runtime.Remoting;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MPic.Utility
{
    public static class EmailHelper
    {
        /// <summary>
        /// 校验是否是邮箱
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {
            Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
            return r.IsMatch(email);
        }

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="reciver"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public static void SendEmail(string reciver, string title, string content)
        {
            try
            {
                var emailAcount = "izhaofu@163.com";// ConfigurationManager.AppSettings["EmailAcount"];
                var emailPassword = "lzf,920925";// ConfigurationManager.AppSettings["EmailPassword"];
                MailMessage message = new MailMessage();
                //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
                MailAddress fromAddr = new MailAddress(emailAcount, "MPic");
                message.From = fromAddr;
                //设置收件人,可添加多个,添加方法与下面的一样
                message.To.Add(reciver);
                //设置抄送人
                //message.CC.Add("qwe123@163.com");
                //设置邮件标题
                message.Subject = title;
                //设置邮件内容
                message.Body = content;
                message.SubjectEncoding = Encoding.UTF8;
                message.BodyEncoding = Encoding.UTF8;
                message.Priority = MailPriority.High;
                message.IsBodyHtml = true;
                //设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看,下面是QQ的
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.163.com";
                client.UseDefaultCredentials = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //设置发送人的邮箱账号和密码
                client.Credentials = new NetworkCredential(emailAcount, emailPassword);
                //启用ssl,也就是安全发送
                //client.EnableSsl = true;
                //发送邮件
                client.Send(message);
            }
            catch (Exception e)
            {
                LoggerHelper.Log("【发送邮件】失败", e);
            }
        }

        public static void Send(string email, string title, string content)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "xPhY7mQKBtUd1oP2", "4hdBclUDN8oTBhn7PS4vwKKdqPKrDS");
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendMailRequest request = new SingleSendMailRequest();
            try
            {
                request.AccountName = "mpic@email.mpic.lzhaofu.cn";// "控制台创建的发信地址";
                request.FromAlias = "MPic图床";
                request.AddressType = 1;
                request.TagName = "MPic";
                request.ReplyToAddress = true;
                request.ToAddress = "lzf2015@qq.com";
                request.Subject = title;
                request.HtmlBody = content;
                SingleSendMailResponse httpResponse = client.GetAcsResponse(request);
            }
            catch (System.Runtime.Remoting.ServerException e)
            {
                LoggerHelper.Log(e.Message);
                //e.printStackTrace();
            }
            catch (ClientException e)
            {
                LoggerHelper.Log(e.Message);
                //e.printStackTrace();
            }
        }

        public static void SMTPSend(string email, string title, string content)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();
                mailMsg.To.Add(new MailAddress(email));
                mailMsg.From = new MailAddress("mpic@email.mpic.lzhaofu.cn", "MPic图床");
                // 邮件主题
                mailMsg.Subject = title;
                // 邮件正文内容
                string text = "欢迎使用MPic图床";
                string html = content;
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
                mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
                //邮件推送的SMTP地址和端口
                SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 80);
                // 使用SMTP用户名和密码进行验证
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("mpic@email.mpic.lzhaofu.cn", "L9z2f0y5R3K2d");
                smtpClient.Credentials = credentials;
                //smtpClient.EnableSsl = true;
                smtpClient.Send(mailMsg);
            }
            catch (Exception ex)
            {
                LoggerHelper.Log(ex.Message);
                Console.WriteLine(ex.ToString());
            }
        }

        public static string EmailTemplate()
        {
            var temp = @"<div><table border='0' cellpadding='0' cellspacing='0' width='100%' style='border-collapse: collapse; background-color: rgb(235, 237, 240); font-family:&quot; Alright Sans LP &quot;, &quot; Avenir Next&quot;, &quot; Helvetica Neue&quot;, Helvetica, Arial, &quot; PingFang SC&quot;, &quot; Source Han Sans SC&quot;, &quot; Hiragino Sans GB & quot;, &quot; Microsoft YaHei&quot;, &quot; WenQuanYi MicroHei&quot;,sans-serif; '>
                               <tbody><tr><td>
                                           <table cellpadding='0' cellspacing='0' align='center' width='640'>                              
                                                      <tbody>                              
                                                          <tr>                              
                                                              <td style='height:20px;'></td>                               
                                                           </tr>                               
                                                           <tr>                               
                                                               <td height='10'></td>                                
                                                            </tr>                                
                                                            <tr>                                
                                                                <td>                                
                                                                    <table cellpadding='0' cellspacing='0' width='640'>                                     
                                                                             <tbody>                                     
                                                                                 <tr style = 'line-height: 40px;'>                                      
                                                                                      <td width='80' style='padding-left:290px;'>                                         
                                                                                             <a href = 'http://mpic.lzhaofu.cn' >                                          
                                                                                                  <img src='http://ojalbwkyk.bkt.clouddn.com//image/mpic/pic.png' width='54'>                                             
                                                                                                 </a>                                             
                                                                                             </td>                                             
                                                                                         </tr>                                             
                                                                                     </tbody>                                             
                                                                                 </table>                                             
                                                                             </td>                                             
                                                                         </tr>                                             
                                                                         <tr>                                             
                                                                             <td height = '40'></td>                                              
                                                                          </tr>                                              
                                                                          <tr>                                              
                                                                              <td style='background-color: #fff;border-radius:6px;padding:40px 40px 0;'>                                               
                                                                                   <table>                                               
                                                                                       <tbody>                                               
                                                                                           <tr height='40'>                                                
                                                                                                <td style='padding-left:25px;padding-right:25px;font-size:18px;font-family:'微软雅黑','黑体',arial;'>
                                                                                                     尊敬的MPic用户：
                                                </td>
                                            </tr>
                                            <tr height='15'> 
                                                 <td></td> 
                                             </tr> 
                                             <tr height='30'>  
                                                  <td style='padding-left:55px;padding-right:55px;font-family:'微软雅黑','黑体',arial;font-size:14px;line-height:20px;'>
                                                       您正在注册验证，验证码：<span style='font-size:30px;'>{验证码}</span>，请在10分钟内按页面提示提交验证码，切勿将验证码泄露于他人。
                                                </td>
                                            </tr>
                                            <tr height='20'> 
                                                 <td></ td> 
                                             </tr> 
                                             <tr> 
                                                 <td style='padding-left:55px;padding-right:55px;font-family:'微软雅黑','黑体',arial;font-size:14px;'>
                                                      此致
                                                      <br> MPic图床神器
                                                  </td>  
                                              </tr>  
                                              <tr height='50'>   
                                                   <td></td>   
                                               </tr>   
                                           </tbody>   
                                       </table>   
                                   </td>   
                               </tr>   
                               <tr>   
                                   <td style='height:40px;'></td>    
                                </tr>
                            <tr>
                                <td style = 'height:50px;'></td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div>
    <includetail>
        <img width='1px' height='1px' src='http://mpic.lzhaofu.cn'>
    </includetail>
</div>
"; return temp;
        }
    }
}
